home *** CD-ROM | disk | FTP | other *** search
- /* basename - print last part of a path Authors: B. Garfolo & P. Nelson */
-
- /* Basename - print the last part of a path.
- *
- * For MINIX -- Conforms to POSIX - P1003.2/D10
- * Exception -- it ignores the LC environment variables.
- *
- * Original MINIX author: Blaine Garfolo
- * POSIX rewrite author: Philip A. Nelson
- *
- * POSIX version - October 20, 1990
- * Feb 14, 1991: changed rindex to strrchr. (PAN)
- *
- */
-
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
-
- #define EOS '\0'
-
- int main (int argc, char **argv);
-
- int main(argc, argv)
- /* [<][>][^][v][top][bottom][index][help] */
- int argc;
- char *argv[];
- {
- char *result_string; /* The pointer into argv[1]. */
- char *temp; /* Used to move around in argv[1]. */
- int suffix_len; /* Length of the suffix. */
- int suffix_start; /* Where the suffix should start. */
- int argbeg=1,amiga_mode=0;
-
- /* Check for the correct number of arguments. */
- if ((argc < 2) || (argc > 4)) {
- fprintf(stderr, "Usage: basename [--amiga] string [suffix] \n");
- exit(1);
- }
-
- if (!strcmp(argv[1],"--amiga"))
- {
- argbeg++;
- amiga_mode=1;
- }
-
- /* Check for all /'s */
- for (temp = argv[argbeg]; (*temp == '/') || ((amiga_mode)&&(*temp==':')); temp++) /* Move to next char. */
- ;
- if (*temp == EOS) {
- printf("/\n");
- exit(0);
- }
-
- /* Build the basename. */
- result_string = argv[argbeg];
-
- /* Find the last /'s or :'s if amiga_mode is on */
- temp = strrchr(result_string, '/');
- if ((temp == NULL)&&(amiga_mode)) {
- temp = strrchr(result_string, ':');
- }
-
- if (temp != NULL) {
- /* Remove trailing /'s. */
- while ((*(temp + 1) == EOS) && ((*temp == '/')
- || ((amiga_mode==0)||(*temp == ':'))) ) *temp-- = EOS;
-
- /* Set result_string to last part of path. */
- if ((*temp != '/') && ((amiga_mode==0)||(*temp != ':')))
- { temp = strrchr(result_string, '/');
- if ((temp==NULL)&&(amiga_mode)) temp = strrchr(result_string, ':'); }
-
- if (temp != NULL && ((*temp == '/') || ((amiga_mode==0)||(*temp == ':')) ))
- { result_string = temp + 1; }
- }
-
- /* Remove the suffix, if any. */
- if (argc-amiga_mode > 2) {
- suffix_len = strlen(argv[argbeg+1]);
- suffix_start = strlen(result_string) - suffix_len;
- if (suffix_start > 0)
- if (strcmp(result_string + suffix_start, argv[argbeg+1]) == EOS)
- *(result_string + suffix_start) = EOS;
- }
-
- /* Print the resultant string. */
- printf("%s\n", result_string);
- return(0);
- }
- /* [<][>][^][v][top][bottom][index][help] */
-